package com.whg.demo.biz.ureport.provider;

import com.bstek.ureport.provider.report.ReportFile;
import com.bstek.ureport.provider.report.ReportProvider;
import com.whg.demo.mapper.ReportFileModelMapper;
import com.whg.demo.model.ReportFileModel;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author whg
 * @create 2023/3/15 10:15
 * @description：
 */
@Component
public class TemplateReportProvider implements ReportProvider {
    private static final String NAME = "浩刚文件服务器系统";
    //特定前缀，ureport底层调用getPrefix方法来获报表操作的Provier类
    private String prefix = "whg:";
    //是否禁用
    private boolean disable = false;
    @Resource
    private ReportFileModelMapper reportFileModelMapper;
    //加载表报
    public InputStream loadReport(String fileName) {
        fileName = getNoPrefixName(fileName);
        ReportFileModel param = new ReportFileModel();
        param.setFilename(fileName);
        ReportFileModel model = reportFileModelMapper.selectOne(param);
        byte[] content = new byte[0];
        if (model != null) {
            content = model.getContent();
        }
        return new ByteArrayInputStream(content);
    }
    //删除表报
    public void deleteReport(String fileName) {
        if (fileName != null){
            fileName = getNoPrefixName(fileName);
            ReportFileModel param = new ReportFileModel();
            param.setFilename(fileName);
            reportFileModelMapper.delete(param);
        }
    }
    //查询所有表报
    public List<ReportFile> getReportFiles() {
        List<ReportFileModel> fileModels = reportFileModelMapper.selectAll();
        List<ReportFile> reportFiles = new ArrayList<ReportFile>();
        for (ReportFileModel model : fileModels){
            reportFiles.add(new ReportFile(model.getFilename(), model.getUptime()));
        }
        return reportFiles;
    }
    //保存表报
    public void saveReport(String fileName, String content) {
        fileName = getNoPrefixName(fileName);
        ReportFileModel param = new ReportFileModel();
        param.setFilename(fileName);
        ReportFileModel model = reportFileModelMapper.selectOne(param);
        Date date = new Date();
        if (model == null){
            model = new ReportFileModel();
            model.setContent(content.getBytes());
            model.setCrtime(date);
            model.setUptime(date);
            model.setFilename(fileName);
            reportFileModelMapper.insert(model);
        }else {
            model.setContent(content.getBytes());
            model.setUptime(date);
            reportFileModelMapper.updateByPrimaryKey(model);
        }
    }
    /**
     * 获取没有前缀的文件名
     * @param name
     * @return
     */
    private String getNoPrefixName(String name){
        if(name.startsWith(prefix)){
            name = name.substring(prefix.length(), name.length());
        }
        return name;
    }
    public String getName() {
        return NAME;
    }
    public boolean disabled() {
        return disable;
    }
    public String getPrefix() {
        return prefix;
    }
}
